package com.littleHan.brushQuestions.web.userCenter.controller;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.*;
import com.littleHan.brushQuestions.web.start.pojo.DicValue;
import com.littleHan.brushQuestions.web.start.pojo.GeneralUser;
import com.littleHan.brushQuestions.web.start.pojo.Role;
import com.littleHan.brushQuestions.web.userCenter.pojo.Announcement;
import com.littleHan.brushQuestions.web.userCenter.pojo.DicType;
import com.littleHan.brushQuestions.web.userCenter.pojo.Pagination;
import com.littleHan.brushQuestions.web.userCenter.pojo.vo.GeneralUserRoleVo;
import com.littleHan.brushQuestions.web.userCenter.pojo.vo.SearchVo;
import com.littleHan.brushQuestions.web.userCenter.service.AdminUserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adminUserCenter")
public class AdminUserCenterController {

    @Autowired
    private AdminUserCenterService adminUserCenterService;

    /**
     * 获取普通用户列表
     * @param pagination 分页实例
     * @return
     */
    @PostMapping("/getGeneralUserListJ")
    public String getGeneralUserList(@RequestBody Pagination pagination){
        Map<String,Object> map=adminUserCenterService.getGeneralUserList(pagination);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取普通用户列表成功").setData(map));
    }

    /**
     * 修改普通用户的封禁状态
     * @param generalUser 携带普通用户id和封禁状态
     * @return
     * @throws FailedToUpdateException
     */
    @PostMapping("/banAndLiftJ")
    public String banAndLift(@RequestBody GeneralUser generalUser)throws FailedToUpdateException {
        adminUserCenterService.banAndLift(generalUser);
        return JSON.toJSONString(new Result().setCode(200).setMessage("封禁状态修改成功"));

    }

    /**
     * 获取权限列表
     * @return
     */
    @GetMapping("/getRoleListJ")
    public String getRoleList(){
        List<Role> roleList=adminUserCenterService.getRoleList();
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取权限列表成功").setData(roleList));
    }

    /**
     * 获取该用户有哪些权限
     * @param generalUser 携带普通用户id的实例
     * @return
     * @throws FailedToLoadException
     */
    @PostMapping("/getGeneralUserRoleJ")
    public String getGeneralUserRole(@RequestBody GeneralUser generalUser) throws FailedToLoadException {
        List<String> roleIdList=adminUserCenterService.getGeneralUserRole(generalUser.getId());
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取当前用户已有的权限成功").setData(roleIdList));
    }

    /**
     * 修改当前用户权限(经过删除该用户所有权限，然后再添加改用户的权限)
     * @param generalUserRoleVo 携带权限列表（roleList）和该普通用户id
     * @return
     * @throws FailedSettingException
     */
    @PostMapping("/updateGeneralUserRoleJ")
    public String updateGeneralUserRole(@RequestBody GeneralUserRoleVo generalUserRoleVo)throws FailedSettingException {
        System.out.println(generalUserRoleVo);
        adminUserCenterService.updateGeneralUserRole(generalUserRoleVo);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改当前用户权限成功"));
    }

    /**
     * 获取审核员列表
     * @param pagination 分页对象
     * @return
     */
    @PostMapping("/getAuditorListJ")
    public String getAuditorList(@RequestBody Pagination pagination){
        Map<String,Object> map=adminUserCenterService.getAuditorList(pagination);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取审核员列表成功").setData(map));
    }

    /**
     * 根据roleId和昵称以及用户名搜索满足条件的用户
     * @param searchVo 携带roleId和搜索内容的实例
     * @return
     */
    @PostMapping("/searchGeneralUserJ")
    public String searchGeneralUser(@RequestBody SearchVo searchVo){
        Map<String,Object> map=adminUserCenterService.searchGeneralUser(searchVo);
        return JSON.toJSONString(new Result().setCode(200).setMessage("搜索成功").setData(map));
    }

    /**
     * 获取数据字典中的题型
     * @param pagination 分页对象
     * @return
     */
    @PostMapping("/getDicTypeJ")
    public String getDicType(@RequestBody Pagination pagination)throws FailedToLoadException{
        Map<String,Object> map=adminUserCenterService.getDicType(pagination);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取数据字典题型成功").setData(map));
    }

    /**
     * 获取数据字典中的难度等级
     * @param pagination 分页对象
     * @return
     */
    @PostMapping("/getDicDifficultJ")
    public String getDicDifficult(@RequestBody Pagination pagination)throws FailedToLoadException{
        Map<String,Object> map=adminUserCenterService.getDicDifficult(pagination);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取数据字典题型成功").setData(map));
    }

    /**
     * 获取数据字典中的科目
     * @param pagination 分页对象
     * @return
     */
    @PostMapping("/getDicObjectJ")
    public String getDicObject(@RequestBody Pagination pagination)throws FailedToLoadException{
        Map<String,Object> map=adminUserCenterService.getDicObject(pagination);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取数据字典题型成功").setData(map));
    }

    /**
     * 修改数据字典值
     * @param dicValue 数据字典值对象
     * @return
     */
    @PostMapping("/updateDicValueJ")
    public String updateDicValue(@RequestBody DicValue dicValue)throws FailedToUpdateException{
        adminUserCenterService.updateDicValue(dicValue);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改数据字典成功"));
    }

    /**
     * 根据数据字典id删除数据字典(要经过验证，题目表中是否有题目已经绑定了该类型，如果绑定了就不能删除了)
     * @param dicValue 数据字典实例
     * @return
     * @throws FailedToDeleteException
     */
    @PostMapping("/deleteDicValueJ")
    public String deleteDicValue(@RequestBody DicValue dicValue) throws FailedToDeleteException {
        return adminUserCenterService.deleteDicValue(dicValue.getId());
    }

    /**
     * 获取数据字典类型列表
     * @return
     */
    @PostMapping("/getDicTypeListJ")
    public String getDicTypeList(){
        List<DicType> list= adminUserCenterService.getDicTypeList();
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取数据字典类型成功").setData(list));
    }

    /**
     * 添加数据字典
     * @param dicValue 数据字典值对象
     * @return
     * @throws FailedToInsertException
     */
    @PostMapping("/addDicValueJ")
    public String addDicValue(@RequestBody DicValue dicValue) throws FailedToInsertException{
        adminUserCenterService.addDicValue(dicValue);
        return JSON.toJSONString(new Result().setCode(200).setMessage("新建数据字典成功"));
    }

    /**
     * 获取公告信息
     * @param pagination 分页对象
     * @return
     */
    @PostMapping("/getAnnouncementListJ")
    public String getAnnouncementList(@RequestBody Pagination pagination){
        Map<String,Object> map=adminUserCenterService.getAnnouncementList(pagination);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取公告成功").setData(map));
    }

    /**
     * 根据公告id删除公告
     * @param announcement 携带公告id的公告实例
     * @return
     * @throws FailedToDeleteException
     */
    @PostMapping("/deleteAnnouncementJ")
    public String deleteAnnouncement(@RequestBody Announcement announcement) throws FailedToDeleteException {
        adminUserCenterService.deleteAnnouncement(announcement);
        return JSON.toJSONString(new Result().setCode(200).setMessage("删除公告成功"));
    }

    /**
     * 添加公告信息
     * @param announcement 携带公告内容的公告实例
     * @return
     * @throws FailedToInsertException
     */
    @PostMapping("/addAnnouncementJ")
    public String addAnnouncement(@RequestBody Announcement announcement) throws FailedToInsertException{
        adminUserCenterService.addAnnouncement(announcement);
        return JSON.toJSONString(new Result().setCode(200).setMessage("添加公告成功"));
    }

}
